import {
	apiGetAreaGaishu,
	apiGetArealist,
	apiGetAreaDetail,
	apiGetAreaDetailServerItem,
	apiGetAreaDetailServerItemInfo,
	apiGetAreaSearch,
	apiGetWangDuanByIdAndType,
	apiInsertAreaInfo,
	apiUpdataAreaInfo,
	apiDelArea,
	apiChanelSearch,
	apiChanelDetail,
	apiInsertData,
	apiChannelUpdata,
	apiChanelDel,
	apiGetBaseInfo,
	apiResourceSearch,
	apiRequestCount,
	apiServiceQuality,
	apiAreaAreaTopu,
	apihealth,
	apihealthAll,
	apiPaiMing,
	apiGetAreaTongdao,
	apiDaochu,
	apiAreaPeizhi,
	apiApplyTime,
	apiRequestTotal,
	apiRequestCountTime
} from './apiareaResource';


export const computedGetAreaGaishu = (ev) => {/*获取总体概述*/
			 apiGetAreaGaishu().then((res)=>{
			 	console.log(res);
			 	if(res.data.code==='S100101'){
			 		ev.allGaishu[0].num = res.data.data.tdCount
			 		ev.allGaishu[1].num = res.data.data.ycCount
			 		ev.allGaishu[2].num = res.data.data.wdCount
			 		ev.allGaishu[3].num = res.data.data.qyCount
			 		ev.allGaishu[4].num= res.data.data.fwCount
			 		ev.allGaishu[5].num = res.data.data.yxfwCount
			 		ev.allGaishu[6].num = res.data.data.gxfwCount
			 		ev.allGaishu[7].num = res.data.data.yxgxCount
			 		ev.allGaishu[8].num = res.data.data.yxgxCount
			 		ev.allGaishu[9].num = res.data.data.yxcjCount
			 	}
			 })

};



export const computedPostAreaGaishu=(ev)=>{
	apiGetArealist(ev.area_about_bottom_page).then((res)=>{
				if(res.data.code==='S100101'){
						ev.areaAboutBottom = res.data.data;
						ev.area_about_bottom_length =Math.ceil(res.data.data[0].count/5);
				}
	})
	
}


export const computedGetAreaDetail=(ev)=>{/*获取区域详情上部分*/
	apiGetAreaDetail(ev.detailId).then((res)=>{
				if(res.data.code==='S100101'){
					ev.tongDaoInfo = res.data.data.smChannelList;
					ev.bascInfo = res.data.data;
				}
	})
}

export const computedGetAreaDetail2=(ev)=>{/*获取区域详情上部分2*/
	apiGetAreaDetail(ev.detailId).then((res)=>{
				if(res.data.code==='S100101'){
					ev.formData[0].value=res.data.data.name
					ev.formData[1].value=res.data.data.nodeSn
					ev.formData[2].value=res.data.data.memory
					ev.formData[3].value=res.data.data.diskSize
					ev.formData[4].value=res.data.data.cpuNum
					ev.formData[5].value=res.data.data.segmentName
				}
	})
}


export const computedGetAreaDetail3=(ev)=>{/*获取区域详情上部分3*/
	apiGetAreaDetail(ev.detailId).then((res)=>{
				if(res.data.code==='S100101'){
					ev.formUpdata.name=res.data.data.name
					ev.formUpdata.nodeSn=res.data.data.nodeSn
					ev.formUpdata.memory=res.data.data.memory
					ev.formUpdata.diskSize=res.data.data.diskSize
					ev.formUpdata.cpuNum=res.data.data.cpuNum
					ev.formUpdata.segmentName=res.data.data.segmentName
					ev.formUpdata.segment=res.data.data.segment
					ev.formUpdata.id=ev.detailId;
				}
	})
}


export const computedGetAreaDetailServerItem=(ev)=>{/*获取区域详情服务信息的title*/
	apiGetAreaDetailServerItem(ev.detailId).then((res)=>{
				if(res.data.code==='S100101'){
						ev.serverInfoTitle = res.data.data.businessTypes;
						console.log(ev.serverInfoTitle);
				}
	})
}

export const computedGetAreaDetailServerItemInfo=(ev,businessTypeIds,type,pageNum,pageSize)=>{/*获取区域详情服务信息的detail信息*/
	apiGetAreaDetailServerItemInfo({
		"areaId" :ev.detailId,
		"serviceType":type	,
		"businessTypeIds":[Number(businessTypeIds)],
		"pageNum":pageNum,
		"pageSize":pageSize,
	}).then((res)=>{
				if(res.data.code==='S100101'){
					if(type==1){
						ev.serverInfoItem = res.data.data;
						ev.server_info_item_length = Math.ceil(res.data.data[0].total/4);
					}else if(type==2){
						ev.serverInfoItem2 = res.data.data;
						ev.server_info_item_length2 = Math.ceil(res.data.data[0].total/4);
					}
				
				}
	})
}

export const computedGetAreaSearch=(ev)=>{/*区域管理列表*/
	apiGetAreaSearch(ev.form).then((res)=>{
				if(res.data.code==='S100101'){
					let list = res.data.data.list;

					apiGetWangDuanByIdAndType({
						 	"type":"segment",
						 }).then((res2)=>{
						 	for (let i = 0; i < res2.data.data.length; i++) {
						 		for (let k = 0; k < list.length; k++) {
						 			if(list[k].segment == res2.data.data[i].id){
						 				list[k].segment = res2.data.data[i].name
						 			}
						 		}
						 	}
						 })
					ev.tableData = list;
					ev.total=res.data.data.total;
				}
	})
}

export const  computedGetWangDuanByIdAndType=(ev)=>{/*获取资源网段*/
		 apiGetWangDuanByIdAndType({
						 	"type":"segment",
						 }).then((res2)=>{
						 	if(res2.data.code ==='S100101'){
						 		ev.formAdd[5].radios = res2.data.data
						 	}
						 	
						 })
}


export const computedInsertAreaInfo=(ev,info)=>{/*区域信息插入到数据库*/
	apiInsertAreaInfo(info).then((res)=>{
				if(res.data.code==='S100101'){
					ev.$message.success(res.data.msg);
					
				}else{
					ev.$message.error(res.data.msg);
				}
				ev.dialogVisible1Cancel();
				ev.pageNo=1
	})
}

export const computedUpdataAreaInfo=(ev,info)=>{/*区域信息更新到数据库*/
	apiUpdataAreaInfo(info).then((res)=>{
				if(res.data.code==='S100101'){
					ev.$message.success(res.data.msg);
				}else{
					ev.$message.error(res.data.msg);
				}
				ev.dialogVisible1Cancel();
				ev.pageNo=1
	})
}

export const computedDelArea=(ev,scope)=>{/*区域信息更新到数据库*/
	
	ev.$confirm('是否确定删除?', '删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          apiDelArea({
			id:scope.row.id
			}).then((res)=>{
						if(res.data.code==='S100101'){
							ev.$message.success(res.data.msg);
							ev.search()/*删除数据后，重新获取列表，页面展示就为最新，显示效果好*/
						}else{
							ev.$message.error(res.data.msg);
						}
			})
        })
}


export const computedChanelSearch=(ev)=>{/*区域信息更新到数据库*/
	apiChanelSearch(ev.form).then((res)=>{
		if(res.data.code==='S100101'){
			ev.tableData = res.data.data.list;
			for (let i in ev.tableData){
				ev.tableData[i].type = ev.tableData[i].type===1?'实时接口':'FTP';
				apiGetAreaDetail(ev.tableData[i].aaAreaId).then((res)=>{
					ev.tableData[i].aaAreaId=res.data.data.name
				})
				apiGetAreaDetail(ev.tableData[i].bbAreaId).then((res)=>{
					ev.tableData[i].bbAreaId=res.data.data.name
				})
			}
			ev.total = res.data.data.total;
		}
	})
}


export const computedChanelDetail=(ev)=>{/*区域信息更新到数据库*/
	apiChanelDetail({id:ev.dataOriginId}).then((res)=>{
		if(res.data.code==='S100101'){
			ev.formData[0].value= res.data.data.name;
			ev.formData[1].value= res.data.data.type===1?'实时接口':'FTP';
			apiGetAreaDetail(res.data.data.aaAreaId).then((res2)=>{
					ev.formData[2].value=res2.data.data.name
			})
			apiGetAreaDetail(res.data.data.bbAreaId).then((res3)=>{
					ev.formData[3].value=res3.data.data.name
			})
			ev.formData[4].value = res.data.data.aaAddress;
			ev.formData[5].value = res.data.data.bbAddress;
		}
	})
}

export const computedChanelDetail2=(ev)=>{/*通道信息的详情修改*/
	apiChanelDetail({id:ev.dataOriginId}).then((res)=>{
		if(res.data.code==='S100101'){
					ev.formUpdata.name=res.data.data.name
					ev.formUpdata.aaAddress=res.data.data.aaAddress;
					ev.formUpdata.bbAddress=res.data.data.bbAddress;
					ev.formUpdata.aaAddressBack=res.data.data.aaAddressBack;
					ev.formUpdata.bbAddressBack=res.data.data.bbAddressBack;
					ev.formUpdata.type = res.data.data.type;
					ev.formUpdata.aaAreaId = res.data.data.aaAreaId;
					ev.formUpdata.bbAreaId = res.data.data.bbAreaId;
					ev.formUpdata.businessType = res.data.data.bizType;
					ev.formUpdata.factory = res.data.data.manufacturer;
					ev.formUpdata.id=ev.dataOriginId;
					
		}
	})
}


export const computedGetAreaAll=(ev)=>{/*获取全部区域名字*/
		apiGetAreaSearch({
			name:'',
			pageNum:1,
			pageSize:100000000,
		}).then((res)=>{
			ev.formAdd[3].options =res.data.data.list 
			ev.formAdd[4].options =res.data.data.list 
		})
}

export const computedInsertData=(ev,val)=>{/*注册通道信息*/
		apiInsertData(val).then((res)=>{
			if(res.data.code==='S100101'){
				ev.$message.success(res.data.msg);
				ev.search();
				ev.cleanData = !ev.cleanData;
				ev.closeForm();
				ev.onCancel();
			}else{
				ev.$message.error(res.data.msg);
				ev.closeForm();
			}
		})
}

export const computedChannelUpdata=(ev,val)=>{/*注册通道信息*/
		apiChannelUpdata(val).then((res)=>{
			if(res.data.code==='S100101'){
				ev.$message.success(res.data.msg);
				ev.search();
				ev.closeForm();
			}else{
				ev.$message.error(res.data.msg);
			}
		})
}


export const computedChanelDel=(ev,id)=>{/*注册通道信息*/
		ev.$confirm('是否确定删除?', '删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
	        apiChanelDel(id).then((res)=>{
				if(res.data.code==='S100101'){
					ev.$message.success(res.data.msg);
					ev.search();
				}else{
					ev.$message.error(res.data.msg);
				}
			})
        })
	
}

export const computedResourceSearch=(ev,form)=>{/*共享服务日志和采集服务日志的列表*/
		apiResourceSearch(form).then((res)=>{
			if(res.data.code ==='S100101'){
				ev.tableData = res.data.data.list;
				ev.total = res.data.data.total;
				for (let i in ev.tableData){
					let d1 = new Date(ev.tableData[i].startTime);
					let d2 = new Date(ev.tableData[i].endTime);
				 	ev.tableData[i].fuwuTime = ((d2 - d1) / 1000 / 60/60).toFixed(2)+'小时'//两个时间相差的小时数
					
//					ev.tebalData[i] .responseStatus = ev.tebalData[i] .responseStatus===0?'失败':'成功';
				}
				
			}
			
		})
	
}
export const computedGetAreaTongdao=(ev,param)=>{/*根据区域id获取通道*/
		apiGetAreaTongdao(param).then((res)=>{
			if(res.data.code ==='S100101'){
				ev.Tongdaolist = []
				if(res.data.data.list.length!=0){
					
					ev.Tongdaolist = res.data.data.list;
				}
			}
			
		})
	
}

export const computedDaochu=(ev,param)=>{/*导出接口*/
		apiDaochu(param).then((res)=>{
			if(res.data.code ==='S100101'){
				
			}
			
		})
}

export const computedAreaPeizhi=(ev,param)=>{/*配置详情*/
		let smChannelList = [];
		if(param.form.strategyType==3){
			for (let i = 0; i < param.list.length; i++) {
				smChannelList.push(
					{
						"id":param.list[i].id,   //通道Id
						"weight":param.list[i].status,   //权重
					}
				)
			}
		}
		apiAreaPeizhi(
			{
				"id":ev.dataOriginId,
				"strategyType":param.form.strategyType,
				"smChannelList":smChannelList
			}
		).then((res)=>{
			if(res.data.code==='S100101'){
				ev.$message.success(res.data.msg);
			}else{
				ev.$message.error(res.data.msg);
			}
			ev.dialogVisible1Cancel();
		})
}



/********************区域的图表*******/

export const computedGetBaseInfo=(ev,id)=>{/*获取bsae信息*/
			apiGetBaseInfo(id).then((res)=>{
				if(res.data.code==='S100101'){
					ev.baseInfo = res.data.data;
					ev.dangAn.fuwufang = res.data.data.producerName;
					ev.dangAn.infoTypeNames = res.data.data.infoTypeNames
					ev.dangAn.createTime = res.data.data.createTime
					ev.dangAn.pushTime = res.data.data.resourceService.createTime
					ev.paimingOption = res.data.data.businessTypes;
					ev.getPaiMingBegin();
				}
			})
}

export const computedRequestCount=(ev,id)=>{/*获取请求汇总*/
			apiRequestCount(id).then((res)=>{
				if(res.data.code==='S100101'){
					ev.requestAll = res.data.data;
				}
				
			})
}


export const computedServiceQuality=(ev,id,year)=>{/*获取服务质量*/
	ev.optionRadra.radar.indicator=[];
	ev.optionRadra.series[0].data[0].value=[];
	ev.optionRadra.series[0].data[1].value=[];
			apiServiceQuality(id,year).then((res)=>{
				if(res.data.code==='S100101'){
					if(typeof(ev.optionRadra)!="undefined"){
						let maxQuality = 0;/*最大值*/
						for(let i in res.data.data){
							if(res.data.data[i]===null){
								res.data.data[i] = 0;
							}
							if(res.data.data[i]>maxQuality){
								maxQuality = res.data.data[i];
							}
						}
						console.log(res.data.data);
						if(ev.serverType==='share'){
							ev.optionRadra.radar.indicator.push(/*最大值*1.1*/
								{name: '请求方数量', max: maxQuality*1.1},
								{name: '调用总次数', max: maxQuality*1.1},
								{name: '月调用数', max: maxQuality*1.1},
								{name: '平均响应时间', max: maxQuality*1.1},
								{name: '异常次数', max: maxQuality*1.1},
							)
							ev.optionRadra.series[0].data[0].value.push(res.data.data.requesterNum);
							ev.optionRadra.series[0].data[0].value.push(res.data.data.allCount);
							ev.optionRadra.series[0].data[0].value.push(res.data.data.monthCount);
							ev.optionRadra.series[0].data[0].value.push(res.data.data.allRespSecendSum);
							ev.optionRadra.series[0].data[0].value.push(res.data.data.allErrorCount);
							ev.dangAn.allErrorCount = res.data.data.allErrorCount;
							ev.dangAn.allCount = res.data.data.allCount;
							ev.dangAn.requesterNum = res.data.data.requesterNum;
							ev.dangAn.sourceAddress = res.data.data.sourceAddress;
							ev.dangAn.destinationAddress = res.data.data.destinationAddress;
							
							ev.optionRadra.series[0].data[1].value.push(res.data.data.avgRequesterNum);
							ev.optionRadra.series[0].data[1].value.push(res.data.data.avgAllCount);
							ev.optionRadra.series[0].data[1].value.push(res.data.data.avgMonthCount);
							ev.optionRadra.series[0].data[1].value.push(res.data.data.avgAllRespSecendSum);
							ev.optionRadra.series[0].data[1].value.push(res.data.data.avgAllErrorCount);
						}else if(ev.serverType==='collect'){
							ev.optionRadra.radar.indicator.push(/*最大值*1.1*/
								{name: '月均耗时', max: maxQuality*1.1},
								{name: '月异常次数', max: maxQuality*1.1},
								{name: '月采集次数', max: maxQuality*1.1},
								{name: '年总耗时', max: maxQuality*1.1},
								{name: '年异常次数', max: maxQuality*1.1},
							)
							ev.optionRadra.series[0].data[0].value.push(res.data.data.monthResponseSecendSum);
							ev.optionRadra.series[0].data[0].value.push(res.data.data.monthErrorCount);
							ev.optionRadra.series[0].data[0].value.push(res.data.data.monthCount);
							ev.optionRadra.series[0].data[0].value.push(res.data.data.yearResponseSecendSum);
							ev.optionRadra.series[0].data[0].value.push(res.data.data.yearErrorCount);
							
							ev.optionRadra.series[0].data[1].value.push(res.data.data.avgMonthResponseSecendSum);
							ev.optionRadra.series[0].data[1].value.push(res.data.data.avgmonthErrorCount);
							ev.optionRadra.series[0].data[1].value.push(res.data.data.avgmonthCount);
							ev.optionRadra.series[0].data[1].value.push(res.data.data.avgyearResponseSecendSum);
							ev.optionRadra.series[0].data[1].value.push(res.data.data.avgyearErrorCount);
							ev.dangAn.allGatherCount = res.data.data.allGatherCount/*总采集条数*/
							ev.dangAn.allCount = res.data.data.allCount/*总采集次数*/
							ev.dangAn.allErrorCount = res.data.data.allErrorCount/*总异常数*/
							
						}
						
					}
					
					ev.showServerQuarce = true;
				}
				
			})
}

export const copmutedPaiMing = (ev,id,businessTypeId)=>{/*同类排名*/
//	apiPaiMing(id).then((res)=>{
//		
//	})
	apiPaiMing(id,businessTypeId).then((res)=>{
		if(res.data.code ==='S100101'){
			for (let i = 0; i < res.data.data.businessTypeList.length; i++) {
				if(res.data.data.businessTypeList[i].id == businessTypeId){
					ev.paimingForm.businessTypeName = res.data.data.businessTypeList[i].name;
					break;
				}
			}
			ev.paimingForm.businessTypeServiceCount = res.data.data.businessTypeServiceCount;
			ev.paimingForm.preMonthOrder = res.data.data.preMonth.order;
			ev.paimingForm.currentMonthOrder = res.data.data.currentMonth.order;
			ev.paimingForm.riseCount = res.data.data.riseCount;
			ev.paimingForm.riseRate = res.data.data.riseRate;
		}
	})
}

export const computedAreaAreaTopu=(ev)=>{/*获取拓扑图*/
	apiAreaAreaTopu(ev.arae_topu_page).then((res)=>{
			if(res.data.code ==='S100101'){
				ev.tuopuItem = res.data.data;
				ev.arae_topu_length = Math.ceil(res.data.data[0].count/4);
			}
	})
}

export const computedHealth=(ev,id,year,month)=>{/*健康日志*/
	ev.optionLine2.series[1].data=[];
	apihealth(
		{
			"id":id,
			"year":year,
			"month":month
		}
	).then((res)=>{
			if(res.data.code ==='S100101'){
				for(let i=1;i<=31;i++){
					let empty = true;
					for(let k in res.data.data){
						if(res.data.data[k].day==i){
							ev.optionLine2.series[1].data.push(res.data.data[k].errorCount)
							empty = false;
						}
					}
					if(empty){
						ev.optionLine2.series[1].data.push("0");
					}
				}
			}
	});
	apihealthAll(
		{
			"id":id,
			"year":year,
			"month":month
		}
		
	).then((res)=>{
			if(res.data.code ==='S100101'){
				for(let i=1;i<=31;i++){
					let empty = true;
					for(let k in res.data.data){
						if(res.data.data[k].day==i){
							ev.optionLine2.series[0].data.push(res.data.data[k].errorCount)
							empty = false;
						}
					}
					if(empty){
						ev.optionLine2.series[0].data.push("");
					}
				}
				ev.ShowHealth=true;
			}
	})
}


export const  computedApplyTime=(ev,id,year)=>{
	ev.optionLine.series[0].data=[];
	apiApplyTime(id,year).then((res)=>{
		if(res.data.code= 'S100101'){
			let month = ['1','2','3','4','5','6','7','8','9','10','11','12'];
			let count = 0;
			for (let i = 0; i < month.length; i++) {
				count = 0;
				for (let k = 0; k < res.data.data.length; k++) {
					if(res.data.data[k].month === month[i]){
						count = res.data.data[k].monthCount
					}
				}
				ev.optionLine.series[0].data.push(count);
			}
		}
	})
}


export const  computedRequestTotal=(ev,id)=>{
	apiRequestTotal(id).then((res)=>{
		if(res.data.code= 'S100101'){
			let color = '';
			for (let i = 0; i < res.data.data.length; i++) {
//				res.data.data[i]
				color = bg1(ev.optionPie.color);
				
				ev.optionPie.series[0].data.push(
					{
						"name":res.data.data[i].requesterName+'_'+res.data.data[i].requesterId,
						"value":res.data.data[i].count
					}
				);
				ev.optionPie.color.push(color);
				ev.requestTotalLi.push(
					{
						"requesterName":res.data.data[i].requesterName,
						"color":color
					}
				)
			}
			ev.showRequestTotal=true;
		}
	})
}


export const  computedRequestCountTime=(ev,id,year)=>{
	ev.optionBar.series[0].data=[];
	apiRequestCountTime(id,year).then((res)=>{
		if(res.data.code= 'S100101'){
			let month = ['1','2','3','4','5','6','7','8','9','10','11','12'];
			let count = 0;
			for (let i = 0; i < month.length; i++) {
				count = 0;
				for (let k = 0; k < res.data.data.length; k++) {
					if(res.data.data[k].createMonth == month[i]){
						count = res.data.data[k].allCount
					}
				}
				ev.optionBar.series[0].data.push(count);
			}
		}
	})
}


export const computedDanganGuiji = (ev,id)=>{
	const roles = ['来源地址', '注册人', '注册时间', 
	'采集数量', '采集次数','异常次数','目的地址'];
	
	let links = [], data = [];
	
	roles.map((v, k) => {
	    var num = generateRandom(1,2)
		    if(k > 0) {
		        data.push({
		            name: v,
		            value: num,
		            "draggable": "true",
		            category: 1,
		            
		        })
		        links.push({
		            source: 0,
		            target: k,
		            value: num
		        })
		    } else {
		        data.push({
		            name: v,
		            value: '',
		            category: 1,
		            "draggable": "true",
		            symbolSize: 100,
		            itemStyle:{
		                color:"#0892f7"
		            }
		            
		        })
		    }
	})
	
	ev.optionContect.series[0].links=links;
	ev.optionContect.series[0].data=data;
	ev.showOptionContect =true;
}



function generateRandom(min, max) {/*获取随机数*/
    return (min + Math.round(Math.random() * (max - min)))
}


function bg1(colorArr){/*随机颜色*/
	      return '#'+Math.floor(Math.random()*0xffffff).toString(16);
}